package com.linkoog.devtools.http.raw_index;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.linkoog.devtools.http.parser.HttpServiceParser;
import com.linkoog.devtools.metadata.raw_index.ClassMetadata;
import com.linkoog.devtools.metadata.raw_index.ClassMetadataRawFileIndex;
import com.linkoog.devtools.raw_index.core.PsiClassRawFileIndex;
import com.linkoog.devtools.utils.PsiElementUtil;
import com.linkoog.devtools.utils.VirtualFileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

public class HttpServiceClassRawFileIndex extends PsiClassRawFileIndex<HttpServiceClass> {

    public static final Key<Set<PsiElement>> key = Key.create("DevTools.HttpServiceClassRawFileIndex.Key");

    public HttpServiceClassRawFileIndex(Project project) {
    }

    public static HttpServiceClassRawFileIndex getInstance(Project project){
        return EXTENSION_POINT_NAME.findExtension(HttpServiceClassRawFileIndex.class, project);
    }

    @Override
    public boolean isLasy() {
        return false;
    }

    @Override
    public Key<Set<PsiElement>> getPsiElementKey() {
        return key;
    }

    @Override
    public File getCacheFile(Project project) throws IOException{
        return new File(project.getBasePath() + "/.idea/index-data/HttpClass.idx");
    }

    @Override
    public Class<HttpServiceClass> getCacheClass(Project project) {
        return HttpServiceClass.class;
    }

    @Override
    public HttpServiceClass resolve(PsiClass psiClass) {
        if (!isInit()) return null;
        if (psiClass == null) return null;
        if (psiClass.getQualifiedName() == null) return null;

        final List<HttpServiceParser> extensionList = HttpServiceParser.EXTENSION_POINT_NAME.getExtensionList();
        if (extensionList.isEmpty()) return null;


        for (HttpServiceParser httpServiceParser : extensionList) {
            if (isSupport(httpServiceParser, psiClass)) {
                ClassMetadata classMetadata = ClassMetadataRawFileIndex.getInstance(project).getCache(psiClass);
                if (classMetadata == null) return null;

                final VirtualFile virtualFile = psiClass.getContainingFile().getOriginalFile().getVirtualFile();
                final long timestamp = VirtualFileUtils.getTimestamp(virtualFile);

                String moduleFilePath = PsiElementUtil.resolveModuleFilePath(psiClass);
                HttpServiceClass httpServiceClass = new HttpServiceClass(virtualFile.getUrl(), psiClass.getTextOffset(), psiClass.getQualifiedName(),moduleFilePath, httpServiceParser.getSourceType());
                httpServiceClass.initPsiElement(psiClass.getProject(), psiClass);
                httpServiceClass.setJavaDoc(classMetadata.getJavaDoc());
                httpServiceClass.setSwaggerComment(classMetadata.getSwaggerComment());
                httpServiceClass.setTimestamp(timestamp);
                return httpServiceClass;
            }
        }

        return null;
    }

    private boolean isSupport(HttpServiceParser httpServiceParser, PsiClass psiClass){
        final PsiMethod[] psiMethods = psiClass.getAllMethods();
        for (PsiMethod psiMethod : psiMethods) {
            if (httpServiceParser.support(psiMethod)) {
                return true;
            }
        }
        return false;
    }




}
